package me.moscrop.RootCommand.commands;

import me.moscrop.RootCommand.utilities.Chat;
import me.moscrop.RootCommand.utilities.Utility;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

public class Weather {
		
	public enum types
	{
		SUN, RAIN, THUNDER, STORM, LIGHTNING, NULLVALUE;

		private static types available(String str)
	    {
	        try {
	            return valueOf(str.toUpperCase());
	        } 
	        catch (Exception ex) {
	            return NULLVALUE;
	        }
	    }   
	}
	
	public static boolean lightning(CommandSender sender, Command command, String label, String[] args) {
		if (!(sender instanceof Player)) {
			sender.sendMessage(" Command must be run in game.");
			return true;
		}
		
		Player player = (Player) sender;
		Location location = null;
		
		if (args.length == 0) {
			Block block = player.getTargetBlock(null, 200);
			if(block != null) {
				location = block.getLocation();
			}
		} else {
			Player target = sender.getServer().getPlayer(args[0]);
			if(target != null) {
				location = target.getLocation();
				Chat.messageAll(player, ChatColor.DARK_GRAY + " has struck " + ChatColor.WHITE + args[0]);
			} else {
				return false;
			}
		}
		
		player.getWorld().strikeLightning(Utility.getSafeLocationAt(location));
		
		return true;		
	}
		
	public static Boolean weather(CommandSender sender, Command command, String label, String[] args) {		
		if (!(sender instanceof Player)) {
			sender.sendMessage(" Command must be run in game.");
			return true;
		}
		
		World world;
		Player player = (Player) sender;
		int duration;	
				
		if (args.length >= 2) {
			world = Bukkit.getWorld(args[1]);
		} else {
			world = player.getWorld();
		}
		
		if(args.length >= 3) {
			duration = Integer.parseInt(args[2]);
		}else{
			duration = 120;
		}
		
		boolean success = Weather.set(world, args[0], duration);
		if (success) {
			Chat.messageAll(player, ChatColor.DARK_GRAY + " set weather in " + ChatColor.WHITE + world.getName() + ChatColor.DARK_GRAY + " to " + ChatColor.WHITE + args[0]);							
		}
		return success;
	}
	
	public static boolean set(World world, String type, int duration) {	
		switch (types.available(type)) {	
			case SUN:
				world.setStorm(false);
				world.setThundering(false);
				//world.setWeatherDuration(duration * 20);
				return true;
		
			case RAIN:
				world.setStorm(true);
				world.setThundering(false);
				world.setWeatherDuration(duration * 20);
				return true;
								
			case THUNDER:
			case STORM:
				world.setStorm(true);
				world.setThundering(true);
				world.setWeatherDuration(duration * 20);
				return true;
		
			default:
				return false;
		}
	}
	
			

}
